﻿@using System.Globalization
@model  Model.EtlSourceSystemInfo
@{
    ViewBag.Title = "更新数据源表信息";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{
    <style>
        .deleteStyle
        {
            width: 150px;
        }
        .nocss table
        {
            border: 1px solid #8DB2E3;
        }
        .nocss th
        {
            border: solid #ffffff 0px;
            height: 25px;
            background-color: #ffffff;
            text-align: right;
            width: 15%;
            font-weight: normal;
        }
        .nocss td
        {
            border: solid #ffffff 0px;
            height: 25px;
            background-color: #ffffff;
            text-align: left;
            width: 30%;
        }
        .tip_down
        {
            margin-top: 3px;
            border-top: 8px solid #D2E0F2;
        }
        .tip_over
        {
            border: 1px solid #D2E0F2;
            cursor: pointer;
        }
        .tip_out
        {
            border-bottom: 1px solid #ffffff;
        }
        .tip_downimg, .WDCList
        {
            display: none;
        }
        .tipcontentInfo
        {
            margin-top: 3px;
            display: none;
        }
    </style>
    <script type="text/javascript">
        var UrlArray = $.getUrlArray();
        var retDBData = new Array();
        var doAction = { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
            var add = true; ;
            for (var j = 0; j < retDBData.length; j++) {
                if (retDBData[j].GUID == rowData.GUID) {
                    add = false;
                }
            }
            if (add) {
                retDBData.push(rowData);
            }
            var acDB = "<select onchange='DBAction(this.options.selectedIndex," + rowIndex + ",\"" + value + "\"," + rowData.Status +
            ")' ><option value=''>-请选择-</option><option >修改</option><option >删除</option>";
            if (rowData.Status == 0)
                acDB += "<option >启用</option>";
            else
                acDB += "<option >禁用</option>";
            return acDB + "</select>";
        }
        };
        var sqlcolumns = [
                    { field: 'Host', title: '地址', width: 20 },
                    { field: 'Server', title: '数据库名', width: 20 },
                    { field: 'User', title: '用户名', width: 10 },
                    { field: 'Description', title: '描述', width: 32 },
                    { field: 'Status', title: '状态', width: 8, align: 'center', formatter: function (value, rowData, rowIndex) {
                        if (value == 0)
                            return "<font color='#ff0000'>禁用</font>";
                        else
                            return "启用";
                    }
                    },
                    doAction
            ];
        var oracolumns = [
                    { field: 'Host', title: '地址', width: 20 },
                    { field: 'Server', title: '数据库名', width: 15 },
                    { field: 'User', title: '用户名', width: 15 },
                    { field: 'Part', title: '端口', width: 10 },
                    { field: 'Description', title: '描述', width: 22 },
                    { field: 'Status', title: '状态', width: 8, align: 'center', formatter: function (value, rowData, rowIndex) {
                        if (value == 0)
                            return "<font color='#ff0000'>禁用</font>";
                        else
                            return "启用";
                    }
                    },
                    doAction
            ];
        var filecolumns = [
                    { field: 'FileUrl', title: '地址', width: 30 },
                    { field: 'FileUser', title: '用户名', width: 15 },
                    { field: 'Description', title: '描述', width: 37 },
                    { field: 'Status', title: '状态', width: 8, align: 'center', formatter: function (value, rowData, rowIndex) {
                        if (value == 0)
                            return "<font color='#ff0000'>禁用</font>";
                        else
                            return "启用";
                    }
                    },
                    doAction
            ];
        var opts = {
            height: 380,
            rows: 100,
            idField: "GUID",
            para: { TypeID: UrlArray["id"], dbName: encodeURI(UrlArray["dbName"]) },
            checkbox: true,
            columns: sqlcolumns
        };
        function ChangeSourceType() {
            if ($("#ddlSourceType").val() == "1" || $("#ddlSourceType").val() == "2" || $("#ddlSourceType").val() == "4" || $("#ddlSourceType").val() == "8") {
                $(".WDCList").hide();
            } else
                $(".WDCList").show();
            retDBData = [];
            var data = { "total": 0, "rows": retDBData };
            opts["data"] = data;
            $("#DataGridDB").dg(opts);
            retWDCData = [];
            var dataWDC = { "total": 0, "rows": retWDCData };
            optsWDC["data"] = dataWDC;
            $("#DataGridWDC").dg(optsWDC);
        }
        function DBAction(i, v, id, s) {
            if (i == 1)
                showModalMany('AddDB', '/SystemConfiguration/AddDB?id=' + v + '&sourceType=' + $("#ddlSourceType").val(), '610', '225', AddDBInfo);
            else if (i == 2)
                removeDB(v);
            else if (i == 3)
                UpStatus(id, s);

            $("#DataGridDB").dg(opts);
        }
        function AddDB() {
            showModalMany('AddDB', '/SystemConfiguration/AddDB?sourceType=' + $("#ddlSourceType").val(), '610', '225', AddDBInfo);
        }
        function UpStatus(id, status) {
            if (id.length == 36) {
                $.post("/SystemConfiguration/UpDBStatus", { DBID: id, Status: status, dbName: encodeURI(UrlArray["dbName"]) }, function (res) {
                    if (res == "1") {
                        alert("操作成功！");
                        $("#DataGridDB").dg(opts);
                    } else
                        alert("操作失败！" + res);
                });
            } else {
                retDBData[id].Status = status == 0 ? 1 : 0;
                var data = { "total": 0, "rows": retDBData };
                opts["data"] = data;
                $("#DataGridDB").dg(opts);
            }
        }
        function initDBCol(mtp) {
            var tp = $("#ddlSourceType").val();
            if (mtp)
                tp = mtp;
            switch (tp) {
                case "1":
                case "4":
                    opts.columns = sqlcolumns;
                    break;
                case "2":
                    opts.columns = oracolumns;
                    break;
                case "3":
                case "5":
                case "6":
                    opts.columns = filecolumns;
                    break;
            }
        }
        function AddDBInfo(v) {
            initDBCol();
            if (v["id"] == "" || v["id"] == -1) {
                v["GUID"] = retDBData.length;
                v["Status"] = 1;
                retDBData.push(v);
            } else
                retDBData[v["id"]] = v;

            var data = { "total": 0, "rows": retDBData };
            opts["data"] = data;
            $("#DataGridDB").dg(opts);
        }
        function removeDB(i) {
            if (confirm("确定要删除吗？")) {
                retDBData.removeAt(i);
                var data = { "total": 0, "rows": retDBData };
                opts["data"] = data;
                $("#DataGridDB").dg(opts);
            }
        }
        function DelDB() {
            if (confirm("确定要删除吗？")) {
                var j = 1;
                $('#DataGridDB').find(":checkbox").each(function (i, d) {
                    if (i == 0)
                        return;
                    if (d.checked) {
                        retDBData.removeAt(i - j);
                        j++;
                    }
                });
                var data = { "total": 1, "rows": retDBData };
                opts["data"] = data;
                $("#DataGridDB").dg(opts);
            }
        }
        //----------Add DB end---------------- 

        //----------Add WDC begin---------------- 
        var retWDCData = new Array();
        var WildCardData = new Array();
        var optsWDC = {
            height: 380,
            rows: 100,
            idField: "GUID",
            para: { systemID: UrlArray["systemID"], dbName: encodeURI(UrlArray["dbName"]) },
            checkbox: true,
            columns: [
                    { field: 'WildCard', title: '通配符', width: 30 },
                    { field: 'FileExt', title: '扩展名', width: 30 },
                    { field: 'ExtractOrder', title: '抽取顺序', width: 30, formatter: function (value, rowData, rowIndex) {
                        if (value == 1) {
                            return "时间";
                        } else
                            return "名称";
                    }
                    },
                    { field: 'GUID', title: '操作', width: 10, align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var add = true; ;
                            for (var j = 0; j < retWDCData.length; j++) {
                                if (retWDCData[j].GUID == rowData.GUID) {
                                    add = false;
                                }
                            }
                            if (add) {
                                retWDCData.push(rowData);
                                WildCardData.push(rowData.WildCard);
                            }
                            var acWDC = "<select onchange='WDCAction(this.options.selectedIndex,\"" + value + "\",\"" + rowIndex + "\")' ><option value=''>-请选择-</option>";
                            return acWDC + "<option >修改</option><option >删除</option></select>"
                        }
                    }
            ]
        };
        function WDCAction(i, v, index) {
            if (i == 1)
                showModalMany('AddCB', '/SystemConfiguration/AddWDC?id=' + v + '&index=' + index, '610', '205', AddWDCInfo);
            else
                removeWDC(index);
        }
        function AddWDC() {
            showModalMany('AddCB', '/SystemConfiguration/AddWDC?dbType=' + $("#ddlSourceType").val(), '610', '205', AddWDCInfo);
        }
        function AddWDCInfo(v) {
            if (v["Index"] == "") {
                retWDCData.push(v);
            } else
                retWDCData[v["Index"]] = v;
            var data = { "total": 0, "rows": retWDCData };
            optsWDC["data"] = data;
            $("#DataGridWDC").dg(optsWDC);
        }
        function removeWDC(i) {
            if (confirm("确定要删除吗？")) {
                retWDCData.removeAt(i);
                var data = { "total": 0, "rows": retWDCData };
                optsWDC["data"] = data;
                $("#DataGridWDC").dg(optsWDC);
            }
        }
        function DelWDC() {
            if (confirm("确定要删除吗？")) {
                var j = 1;
                $('#DataGridWDC').find(":checkbox").each(function (i, d) {
                    if (i == 0)
                        return;
                    if (d.checked) {
                        retWDCData.removeAt(i - j);
                        j++;
                    }
                });
                var data = { "total": 1, "rows": retWDCData };
                optsWDC["data"] = data;
                $("#DataGridWDC").dg(optsWDC);
            }
        }
        //----------Add WDC end---------------- 
        //----------Add CB begin---------------- 
        var retCBData = new Array();
        var optsCB = {
            height: 380,
            rows: 100,
            idField: "GUID",
            para: { systemID: UrlArray["systemID"], dbName: encodeURI(UrlArray["dbName"]) },
            checkbox: true,
            columns: [
                    { field: 'CallBackType', title: '回调类型', width: 45, formatter: function (value, rowData, rowIndex) {
                        if (value == 1)
                            return "存储过程";
                        else
                            return "HTTP回调";
                    }
                    },
                    { field: 'CallBackName', title: '回调名称', width: 45 },
                    { field: 'GUID', title: '操作', width: 10, align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var add = true; ;
                            for (var j = 0; j < retCBData.length; j++) {
                                if (retCBData[j].GUID == rowData.GUID) {
                                    add = false;
                                }
                            }
                            if (add) {
                                retCBData.push(rowData);
                            }
                            var acCB = "<select onchange='CBAction(this.options.selectedIndex," + rowIndex + ")' ><option value=''>-请选择-</option>";
                            return acCB + "<option >修改</option><option >删除</option></select>"
                        }
                    }
            ]
        };

        function CBAction(i, v) {
            if (i == 1)
                AddCB(v);
            else
                removeCB(v);
            $("#DataGridCB").dg(optsCB);
        }
        function AddCB(v) {
            showModalMany('editCB', '/SystemConfiguration/AddCallBack?id=' + v + '&dbType=' + $("#ddlSourceType").val(), '620', '205', AddCBInfo);
        }
        function AddCBInfo(v) {
            if (!v["GUID"]) {
                v["GUID"] = retCBData.length;
                retCBData.push(v);
            } else {
                retCBData[v["GUID"]] = v;
            }
            var data = { "total": 0, "rows": retCBData };
            optsCB["data"] = data;
            $("#DataGridCB").dg(optsCB);
        }
        function DelCB() {
            if (confirm("确定要删除吗？")) {
                var j = 1;
                $('#DataGridCB').find(":checkbox").each(function (i, d) {
                    if (i == 0)
                        return;
                    if (d.checked) {
                        retCBData.removeAt(i - j);
                        j++;
                    }
                });
                var data = { "total": 1, "rows": retCBData };
                optsCB["data"] = data;
                $("#DataGridCB").dg(optsCB);
            }
        }
        function removeCB(i) {
            if (confirm("确定要删除吗？")) {
                retCBData.removeAt(i);
                var data = { "total": 0, "rows": retCBData };
                optsCB["data"] = data;
                $("#DataGridCB").dg(optsCB);
            }
        }
        //----------Add CB end---------------- 
        //----------Add Contact ----------------
        function AddContact() {
            showModalMany('AddContact', '/SystemConfiguration/UserList?dbName=' + encodeURI(UrlArray["dbName"]), '900', '500', AddContactInfo);
        }
        var UserData = new Array();
        function AddContactInfo(v) {
            for (var i = 0; i < v.length; i++) {
                var userid = v[i].Guid;
                if (UserData.indexOf(userid) == -1) {
                    UserData.push(userid);
                    var userInfo = "工号:" + v[i].EmployeeCode + "\n用户名:" + v[i].Username + "\n公司:" + v[i].CompanyName + "\n电话:" + v[i].OfficeTel + "\nEmail:" + v[i].OfficeEmail;
                    $("#selected").append("<span class='s3 s3" + userid + "'><input type='checkbox' class='" + userid + "' value='" + v[i].Username + "'/><a title='" + userInfo + "'>" + v[i].EmployeeName +
                "</a><span class='s2 s2" + userid + "'' onclick='removeContact(\"" + userid + "\")'>&nbsp;X&nbsp;</span></span>");

                }
            }
        }
        function removeContact(v) {
            $(".s3" + v).remove();
            UserData.removeValue(v);
        }
        function SendMsg() {
            if ($("#hddGuid").val() == "") {
                alert("请先保存源系统信息。");
                return;
            }
            if (UserData.length == 0) {
                alert("请选择联系人。");
                return;
            }
            var sendUserList = new Array();
            $("#selected").find("input").each(function (i, d) {
                if (d.type == "checkbox") {
                    if (d.checked == true) {
                        sendUserList.push(d.value);
                    }
                }
            });
            if (sendUserList.length == 0) {
                alert("请选择接收消息的联系人。");
                return;
            }
            var paras = {};
            paras["systemID"] = $("#hddGuid").val();
            paras["dbName"] = encodeURI(UrlArray["dbName"]);
            paras["target"] = sendUserList.join(",");
            paras["title"] = $("#txt_SourceSysName").val();
            paras["type"] = "1";
            $.post("/SystemConfiguration/SendMsg", paras, function (res) {
                alert(res);
            });
        }
        //----------Add Contact end---------------- 
        var connStrNum = 0;
        $(function () {
            if (UrlArray["systemID"]) {
                var dbType = '@Model.SourceType';
                if (dbType == "1" || dbType == "2" || dbType == "4" || dbType == "8") {
                    $(".WDCList").hide();
                } else
                    $(".WDCList").show();
                initDBCol(dbType);
                opts.url = "/SystemConfiguration/GetEtlSourceDbConfigList?systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]);
                optsWDC.url = "/SystemConfiguration/GetWDCConfigList?systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]);
                optsCB.url = "/SystemConfiguration/GetCallBackConfigList?systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]);
                $.post("/SystemConfiguration/GetEtlSourceUserList/?systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]), function (res) {
                    AddContactInfo(res);
                });
            }
            $("#DataGridDB").dg(opts);
            $("#DataGridWDC").dg(optsWDC);
            $("#DataGridCB").dg(optsCB);
            //--------------where-----------
            $.post("/SystemConfiguration/BindDropwown/?keyName=SourceType", function (result) {
                var arr_sel = new Array();
                if (result != "") {
                    for (var i = 0; i < result.length; i++) {
                        arr_sel.push("<option value='");
                        arr_sel.push(result[i].Value);
                        arr_sel.push("'>");
                        arr_sel.push(result[i].Name);
                        arr_sel.push("</option>");
                    }
                    $("#ddlSourceType").append(arr_sel.join(""));
                    $("#ddlSourceType").val("@Model.SourceType");
                }
            });
            if ('@Model.GUID' != "") {
                $("#ddlStatus").val('@Model.Status');
                $("#ddlCallBackType").val('@Model.CallBackType');
            }
        });
        function back() {
            var url = "/SystemConfiguration/EtlSourceList?dbName=" + encodeURI(UrlArray["dbName"]);
            if (UrlArray["DBType"] == "2")
                url += "&DBType=" + UrlArray["DBType"];
            window.location.href = url;
        }
        function SubmitForm() {
            if ($.trim($("#txt_SourceSysName").val()) == "") {
                alert("系统名称不能为空！");
                return;
            }
            if (UserData.length < 1) {
                alert("联系人不能为空！");
                return;
            }
            var sourceType = $("#ddlSourceType").val();
            var copy = false;
            var extList = new Array();
            if (sourceType == "3")//accecc
            {
                copy = true;
                extList.push(".mdb");
                extList.push(".accdb");
            }
            else if (sourceType == "5")//excel
            {
                copy = true;
                extList.push(".xls");
                extList.push(".xlsx");
            }
            if ($('#ff').form("validate")) {
                $("#DBs").val($.toJSON(retDBData));
                $("#WDCs").val($.toJSON(retWDCData));
                $("#CBs").val($.toJSON(retCBData));
                $("#Users").val(UserData.join(","));
                $('#ff').form('submit', {
                    url: "/SystemConfiguration/EtlSourceUpdate?dbName=" + encodeURI(UrlArray["dbName"]) + "&DBType=" + UrlArray["DBType"],
                    success: function (result) {
                        var arrResult = result.split(",");
                        if (arrResult[0] == "1") {
                            if (arrResult.length > 1) {
                                $("#hddGuid").val(arrResult[1]);
                            }
                            if (copy) {
                                for (var i = 0; i < retDBData.length; i++) {
                                    var ftp = retDBData[i]["FileUrl"].toLowerCase();
                                    if (ftp.indexOf("ftp") == 0) {
                                        $.post("/SystemConfiguration/CopyFTPFile", {
                                            sourceID: arrResult[1],
                                            ftpUrl: retDBData[i]["FileUrl"],
                                            fileUser: retDBData[i]["FileUser"],
                                            filePass: retDBData[i]["FilePass"],
                                            wd: WildCardData.join("$"),
                                            extList: extList.join("$")
                                        });
                                    }
                                }
                            }
                            alert("保存成功！");
                            back();
                        } else if (arrResult[0] == "2") {
                            alert("系统名称已存在!");
                        } else {
                            alert(arrResult[0]);
                        }
                    }
                });
            }
        }
    </script>
}
<div style="margin: 5px;">
    <form id="ff" method="post">
    <input type="hidden" id="hddGuid" name="GUID" value="@Model.GUID" />
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/filesave.png" />
            </td>
            <td width="300">
                添加源系统信息
            </td>
            <td align="right">
                <input type="button" class="button" value="保存" onclick="return SubmitForm()" />
                <input type="button" class="button" value="返回" onclick="back()" />
            </td>
        </tr>
    </table>
    <input type="hidden" id="DBs" name="DBs" />
    <input type="hidden" id="WDCs" name="WDCs" />
    <input type="hidden" id="CBs" name="CBs" />
    <input type="hidden" id="Users" name="Users" />
    <table class="table_bg" style="width: 100%;" cellpadding="0" cellspacing="0">
        <tr>
            <td style="text-align: right; width: 150px;">
                <span class="red">*</span>系统名称：
            </td>
            <td colspan="2">
                <input type="text" id="txt_SourceSysName" name="SourceSysName" value="@Model.SourceSysName" maxlength="50" style="width:480px" />
            </td>
        </tr>
        <tr>
            <td valign="top" style="text-align: right; width: 150px;">
                <span class="red">*</span>联系人：
            </td>
            <td style="width: 484px;" id="selected">
            </td>
            <td valign="top">
                <input type="button" class="button" value="添加" onclick="AddContact()" />
                <input type="button" class="button" value="发送消息" onclick="SendMsg()" />
            </td>
        </tr>
    </table>
    <div class="tip_down">
    </div>
    <div class="tip_over" onclick="TipDown()">
        <div class="tip_upimg">
            <img src="/images/tip_up.png" /></div>
        <div class="tip_downimg">
            <img src="/images/tip_down.png" /></div>
    </div>
    <div class="tipcontentInfo">
        <table class="table_bg" style="width: 100%;" cellpadding="0" cellspacing="0">
            <tr>
                <td style="text-align: right; width: 150px;">
                    数据源类型：
                </td>
                <td>
                    <select id="ddlSourceType" name="SourceType" onchange="ChangeSourceType()" style="width: 150px;">
                    </select>
                </td>
            </tr>
            <tr>
                <td style="text-align: right;">
                    状态：
                </td>
                <td>
                    <select id="ddlStatus" name="Status" style="width: 150px;">
                        <option value="2">禁用</option>
                        <option value="1">启用</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td style="text-align: right;" valign="top">
                    描述：
                </td>
                <td>
                    <textarea id="txt_SourceSysDescription" name="SourceSysDescription" rows='3' style="width: 480px">@Model.SourceSysDescription</textarea>
                </td>
            </tr>
        </table>
        <table class="searchArea" width="100%">
            <tr>
                <td valign="top">
                    <div style="max-height: 400px; min-height: 110px; overflow-y: scroll">
                        <table class="listTitle">
                            <tr>
                                <td width="15">
                                    <img src="/images/edit.png" />
                                </td>
                                <td width="300">
                                    数据源连接配置
                                </td>
                                <td align="right">
                                    <input type="button" value="添加" onclick="AddDB();" class="button" />
                                    <input type="button" value="删除" onclick="DelDB();" class="button" />
                                </td>
                            </tr>
                        </table>
                        <table id="DataGridDB" class="pagTable" cellpadding="0" cellspacing="0">
                        </table>
                    </div>
                </td>
            </tr>
            <tr class="WDCList">
                <td valign="top">
                    <div style="max-height: 400px; min-height: 110px; overflow-y: scroll">
                        <table class="listTitle">
                            <tr>
                                <td width="15">
                                    <img src="/images/edit.png" />
                                </td>
                                <td width="300">
                                    通配符配置
                                </td>
                                <td align="right">
                                    <input type="button" value="添加" onclick="AddWDC();" class="button" />
                                    <input type="button" value="删除" onclick="DelWDC();" class="button" />
                                </td>
                            </tr>
                        </table>
                        <table id="DataGridWDC" class="pagTable" cellpadding="0" cellspacing="0">
                        </table>
                    </div>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <div style="max-height: 400px; min-height: 110px; overflow-y: scroll">
                        <table class="listTitle">
                            <tr>
                                <td width="15">
                                    <img src="/images/edit.png" />
                                </td>
                                <td width="300">
                                    回调配置
                                </td>
                                <td align="right">
                                    <input type="button" value="添加" onclick="AddCB('');" class="button" />
                                    <input type="button" value="删除" onclick="DelCB();" class="button" />
                                </td>
                            </tr>
                        </table>
                        <table id="DataGridCB" class="pagTable" cellpadding="0" cellspacing="0">
                        </table>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    </form>
</div>
